﻿using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;
namespace com.fukefu.info.b2b.auto
{
    public class AiLaBa : Information
    {
        string uid = "", guid = "";
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("uname", name);
            pd.Add("upwd", pass);
            pd.Add("coks", "1");
            string html = source.Post("http://www.ailaba.org/chklogin.html", pd.Read());
            if (html.Equals("1"))
            {
                html = source.Get("http://www.ailaba.org/Publish.html");
                uid = Regex.Match(html, "\"uid\" : \"([0-9]+)\"").Groups[1].Value;
                guid = Regex.Match(html, "\"gpid\" : \"([0-9]+)\"").Groups[1].Value;
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            title = tek.Get(title);
            //去除空标签,换行符
            content = Regex.Replace(content, "<p>\\s*</p>", "", RegexOptions.IgnoreCase);
            content = Regex.Replace(content, "\r\n", "", RegexOptions.IgnoreCase);
            InformationResult info = new InformationResult();
            ParametersData pd = new ParametersData();
            string html = source.Get("http://www.ailaba.org/Publish.html");
            pd.Add("userid", uid);
            pd.Add("postid", "0");
            pd.Add("smallClass", site.TypeId[0]);
            pd.Add("Pro", Regex.Match(html, "\"City\",\"([0-9]+)\",\"[0-9]+\"").Groups[1].Value);
            pd.Add("City", Regex.Match(html, "\"City\",\"[0-9]+\",\"([0-9]+)\"").Groups[1].Value);
            pd.Add("titleremtimti", title);
            pd.Add("mark", title);
            pd.Add("content", content);
            pd.Add("backFunction", "$.c.Uploader.finish");
            pd.Add("Pic", UpImage() + "|" + UpImage() + "|" + UpImage());
            pd.Add("PicDesc", "");
            pd.Add("PicPos", "0");
            pd.Add("lastchkgpid", "-1");
            pd.Add("linkman", pif.Get("联系人"));
            pd.Add("phone", pif.Get("手机"));
            pd.Add("btnSubmit", "提交保存");
            html = source.Post("http://www.ailaba.org/pubsave.html", pd.Read(), "http://www.ailaba.org/Publish.html");
            if (html.IndexOf("信息添加成功 - 爱喇叭分类信息网") != -1)
            {
                info.Code = 1;
                info.Content = "http://www.ailaba.org" + Regex.Match(html, "您的信息“<a href=\"([^\"]+)\" target=\"_blank\">" + title + "</a>").Groups[1].Value;
            }
            else if (html.Equals(""))
            {
                info.Code = 3;
                info.Content = "网络异常..";
            }
            else if (Regex.IsMatch(html, "<script>alert\\('([^']+)"))
            {
                info.Content = Regex.Match(html, "<script>alert\\('([^']+)").Groups[1].Value.Replace("\\n", "");
            }
            return info;
        }

        public string UpImage()
        {
            SourceFormData sfd = new SourceFormData("gb2312");
            //头部数据
            sfd.Write("Filename", random.Next(1000) + ".jpg");
            sfd.Write("uid", uid);
            sfd.Write("gpid", guid);
            //发送文件
            sfd.WriteFile("Filedata", RandomPicture.Get());
            //尾部数据
            sfd.Write("Upload", "Submit Query");
            //结尾
            return source.Post("http://www.ailaba.org/uploader/upuserpic.asp", sfd.GetContentType(), sfd.Read(), "");
        }
    }
}
